package com.zhf.servlet;

import com.zhf.service.GradeService;
import com.zhf.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/25 20:59
 */
@WebServlet(name = "DelGradeServlet",urlPatterns = "/delgrade")
public class DelGradeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String classname = (String)request.getParameter("classname");
        System.out.println("删除班级："+classname+" 的信息");

        GradeService gradeService = new GradeService();
        StudentService studentService = new StudentService();
        //检查班级是否存在
        if (gradeService.getGrade(classname)!=null){
            //该班级无学生
            if (!studentService.isStudentNull(classname)){
                boolean b = gradeService.delGrade(classname);
                request.setAttribute("result", b ? "seccess" : "fail");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("delgrade.jsp");
                requestDispatcher.forward(request,response);
            }else {
                //服务器跳转request共享
                request.setAttribute("error","somestudent");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("delgrade.jsp");
                requestDispatcher.forward(request,response);
            }
        }else {
            //服务器跳转request共享
            request.setAttribute("error","noclass");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("delgrade.jsp");
            requestDispatcher.forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
